#include "debug_functions.h"

#ifdef DEBUG

void print_token_node_tree(parsing_ctx *ctx, uint32_t level, token_node *tree)
{
  uint32_t itr;
  token_node *child_itr = NULL;

  fprintf(stdout, "L:%03d", level);
  for (itr = 0; itr < level; itr++) {
    fprintf(stdout, "    ");
  }
  if (tree->value != NULL) {
    fprintf(stdout, "<%s>\n", (char *) tree->value);
  } else {
    fprintf(stdout, "<%s, %d>\n", ctx->gr_token_name[token_value(tree->token)], token_value(tree->token));
  }
  child_itr = tree->child;
  while (child_itr != NULL && child_itr->parent == tree) {
    print_token_node_tree(ctx, level + 1, child_itr);
    child_itr = child_itr->next;
  }
}

void print_vectorized_reduction_tree(parsing_ctx *ctx, uint32_t offset, uint32_t level)
{
  uint32_t i, j, children_num;
  uint16_t value;

  children_num = ctx->reduction_tree[offset + 1]/2;
  value = ctx->reduction_tree[offset];
  fprintf(stdout, "Node@[%d], ", offset); 
  if (ctx->reduction_tree[offset] != GRAMMAR_SIZE) {
    fprintf(stdout, "rule %d, ", value);
  } else {
    fprintf(stdout, "no rule, ");
  }
  fprintf(stdout, "children %d.\n", children_num);

  for (i = 0; i/2 < children_num; i += 2) {
    /* Print indentation. */
    for (j = 0; j < level + 1; ++j) {
      fprintf(stdout, "    ");
    }
    fprintf(stdout, "--Node@[%d].son n.%d via \"%s\" ", offset, i/2, ctx->gr_token_name[token_value(ctx->reduction_tree[offset + i + 2])]);
    fprintf(stdout, "@[%d]\n", ctx->reduction_tree[offset + i + 3]);
    /* Print indentation. */
    for (j = 0; j < level + 1; ++j) {
      fprintf(stdout, "    ");
    }
    print_vectorized_reduction_tree(ctx, ctx->reduction_tree[offset + i + 3], level + 1);
  }
}

#endif
